/**
 * 家系图模板定义
 *
 * @class TemplateSelector
 * @constructor
 * @param {Boolean} isStartupTemplateSelector 
 */

var TemplateSelector = Class.create( {

    initialize: function(isStartupTemplateSelector) {        
        new Ajax.Request(new XWiki.Document('WebHome').getRestURL('objects/PhenoTips.PedigreeClass/index.xml').substring(1), {
            method: 'GET',
            onSuccess: this._onTemplateListAvailable.bind(this)
        });
    },

    /**
     *
     * @method isStartupTemplateSelector
     * @return {Boolean}
     */
    isStartupTemplateSelector: function() {
        return this._isStartupTemplateSelector;
    },

    /**
     *
     * @param response
     * @private
     */
    _onTemplateListAvailable: function(response) {
        this.mainDiv.update();
    },

    /**
     *
     * @param pictureBox
     * @param response
     * @private
     */
    _onTemplateAvailable: function(pictureBox, response) {
        pictureBox.innerHTML = getSubSelectorTextFromXML(response.responseXML, "property", "name", "image", "value").replace(/&amp;/, '&');
        pictureBox.pedigreeData = getSubSelectorTextFromXML(response.responseXML, "property", "name", "data", "value");
        pictureBox.type         = 'internal';
        pictureBox.description  = getSubSelectorTextFromXML(response.responseXML, "property", "name", "description", "value");
        pictureBox.title        = pictureBox.description;


        if (window.SVGSVGElement &&
            document.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#Image", "1.1")) {
            pictureBox.update(getSubSelectorTextFromXML(response.responseXML, "property", "name", "image", "value"));
        } else {
            pictureBox.innerHTML = "<table bgcolor='#FFFAFA'><tr><td><br>&nbsp;" + pictureBox.description + "&nbsp;<br><br></td></tr></table>";
        }
        pictureBox.observe('click', this._onTemplateSelected.bindAsEventListener(this, pictureBox));
    },

    /**
     *
     * @param event
     * @param pictureBox
     * @private
     */
    _onTemplateSelected: function(event, pictureBox) {
        this.dialog.close();
        if (pictureBox.type == 'internal') {
            editor.getSaveLoadEngine().createGraphFromSerializedData(pictureBox.pedigreeData, false /* add to undo stack */, true /*center around 0*/);
        } else if (pictureBox.type == 'simpleJSON') {
            editor.getSaveLoadEngine().createGraphFromImportData(pictureBox.pedigreeData, 'simpleJSON', {}, false /* add to undo stack */, true /*center around 0*/);
        }
    },

    /**
     *
     * @method show
     */
    show: function() {
        this.dialog.show();
    },

    /**
     *
     * @method hide
     */
    hide: function() {
        this.dialog.closeDialog();
    }
});
